#include "getpoint.h"
#include "ui_getpoint.h"

getpoint::getpoint(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::getpoint)
{
    ui->setupUi(this);

    /*控件定义*/
    this->w=new QWidget(this);
    this->wl=new QWidget(this->w);
    this->wr=new QWidget(this->w);
    this->plot=new scene(this->w);
    this->x_index=new QLineEdit(this->w);
    this->o_index=new QLineEdit(this->w);
    this->x_index->setAlignment(Qt::AlignHCenter);
    this->y_index=new QLineEdit(this->w);
    this->y_index->setAlignment(Qt::AlignHCenter);
    this->file=new QFileDialog(this);
    this->save=new QFileDialog(this);
    this->set_x=new QPushButton(this->w);
    this->set_o=new QPushButton(this->w);
    this->set_y=new QPushButton(this->w);
    this->o_index->setAlignment(Qt::AlignHCenter);
    this->image=new QImage();
    this->x_index->setText("输入x单位值");
    this->y_index->setText("输入y单位值");



    this->set_x->setText("确定");
    this->set_o->setText("确定");
    this->set_y->setText("确定");


    this->open=new QAction(this);
    this->ept=new QAction(this);
    this->bar=new QMenuBar(this);
    this->bar->addAction(this->open);
    this->bar->addAction(this->ept);
    this->open->setText("打开");
    this->ept->setText("导出");
    this->w->setGeometry(0,this->bar->height(),this->width(),this->height()-this->bar->height());
    /*控件布局*/

    this->hfirst=new QHBoxLayout(this->w);
    this->w->setLayout(this->hfirst);
    this->hup=new QHBoxLayout(this->w);
    this->hdown=new QHBoxLayout(this->w);
    this->hcenter=new QHBoxLayout(this->w);
    this->right=new QHBoxLayout(this->w);
    this->left=new QVBoxLayout(this->w);
    this->hfirst->addLayout(this->left);
    this->hfirst->addLayout(this->right);
    this->left->addLayout(this->hup);
    this->left->addLayout(this->hcenter);
    this->left->addLayout(this->hdown);
    this->right->addWidget(this->plot);
    this->hup->addWidget(this->set_o);
    this->hup->addWidget(this->o_index);
    this->hcenter->addWidget(this->set_x);
    this->hcenter->addWidget(this->x_index);
    this->hdown->addWidget(this->set_y);
    this->hdown->addWidget(this->y_index);
    this->hfirst->setStretch(1,10);






    connect(this->open,&QAction::triggered,this,&getpoint::openfile);
    connect(this->ept,&QAction::triggered,this,&getpoint::savefile);
    connect(this->set_x,&QPushButton::clicked,this,&getpoint::setx_u);
    connect(this->set_y,&QPushButton::clicked,this,&getpoint::sety_u);

}
getpoint::~getpoint()
{
    delete ui;
}
void getpoint::openfile()
{
    QStringList filenames;
    this->file->setDirectory(".");
    this->file->setWindowTitle("选择图片");
    if(this->file->exec())
    {
        filenames=this->file->selectedFiles();
    }
    if(this->image->load(filenames.at(0)))
    {
        this->plot->clear();
        this->plot->setPixmap(QPixmap::fromImage(*this->image).scaled(this->plot->size()));
        this->plot->tranfig();
    }
    else
    {
      QMessageBox::information(this,"错误","添加失败");
    }
}
void getpoint::savefile()
{
    QStringList filenames;
    this->save->setNameFilter("Test Files (*.txt)");
    this->save->setDirectory(".");
    this->save->setWindowTitle("输出文件");
    this->setinit();
    if(this->save->exec())
    {
       filenames=this->save->selectedFiles();
    }
    this->plot->write(filenames.at(0),this->xo,this->x_u,this->xx,this->yo,this->y_u,this->yy);
}
 void getpoint::sety_u()
 {
     this->y_u=this->y_index->text().toDouble();
 }
 void getpoint::setx_u()
 {
     this->x_u=this->x_index->text().toDouble();
 }


void getpoint::setinit()
{
    this->plot->geto(this->xo,this->yo);
    this->plot->getx(this->xx,this->yx);
    this->plot->gety(this->xy,this->yy);
}
